require(['dragsort'], function () {
    var webRoot = G_WEB_ROOT;

    var urls = {
        queryClassifyAll: webRoot + "/common/queryClassifyAll",
        queryBrandAllByClassifyId: webRoot + "/common/queryBrandAllByClassifyId",
        queryProductAllByBrandId: webRoot + "/common/queryProductAllByBrandId",
        updateShowIndex: webRoot + "/sys/updateShowIndex",
        orderByView: webRoot + "/sys/orderByView",
    };
    var classifyZone = $('.classifyZone .container .row');
    var brandZone = $('.zone2 .container .brandZone');
    var productZone2 = $('.zone2 .container .productZone2');
    var saveBtn = $('.saveZone button');

    /**
     * 分类数据
     *
     */
    function queryClassifyAll() {
        $.post(urls.queryClassifyAll, function (rs) {
            if (rs.status == 200) {
                renderClassify(rs.body);
            }
        });
    }


    /**
     * 渲染分类
     *
     * @param {any} list
     */
    function renderClassify(list) {
        var html = '';
        $.each(list, function (index, item) {
            if (item.name != '首页') {
                html += '<div class="col-xs-6 col-sm-3 classifyItem classifyEvent" data="' + item.id + '" >';
                html += '<p>' + item.name + '</p>';
                html += '</div>';
            }
        });
        classifyZone.append(html);
        classifyEvent();
    }


    /**
     * 分类事件
     *
     */
    function classifyEvent() {
        $(".classifyEvent").on("click", function () {
            var classifyId = $(this).attr("data");
            $.post(urls.queryBrandAllByClassifyId, { "classifyId": classifyId }, function (rs) {
                if (rs.status == 200) {
                    brandZone.empty();
                    productZone2.empty();
                    renderBrand(rs.body);
                }
            });
        })
    }

    /**
     * 渲染品牌
     *
     * @param {any} list
     */
    function renderBrand(list) {
        var html = '';
        html += '<ul class="list-group brandUl">';
        html += '<li class="list-group-item active">品牌</li>';
        $.each(list, function (index, item) {
            html += '<li class="list-group-item brandEvent" data-id=' + item.id + ' data-showIndex=' + item.showIndex + '><span class="badge">排号：' + item.showIndex + '</span>' + item.name + '</li>';
        });
        html += '</ul>';
        brandZone.append(html);
        brandEvent();
        $(".brandUl").dragsort();
    }

    /**
     * 品牌事件
     *
     */
    function brandEvent() {
        $(".brandEvent").on("click", function () {
            var brandId = $(this).attr("data-id");
            $.post(urls.queryProductAllByBrandId, { "brandId": brandId }, function (rs) {
                if (rs.status == 200) {
                    productZone2.empty();
                    renderProduct2(rs.body);
                }
            });
        })
    }

    /**
     * 渲染-第二个商品区
     *
     * @param {any} list
     */
    function renderProduct2(list) {
        var html = '';
        html += '<ul class="list-group productUl2">';
        html += '<li class="list-group-item active">商品</li>';
        $.each(list, function (index, item) {
            html += '<li class="list-group-item"  data-id=' + item.id + ' data-showIndex=' + item.showIndex + '><span class="badge">排号：' + item.showIndex + '</span>' + item.name + '</li>';
        });
        html += '</ul>';
        productZone2.append(html);
        $(".productUl2").dragsort();
    }


    /**
     * 更新排序
     *
     */
    function update() {
        saveBtn.unbind().bind('click', function () {
            var $li = $(".brandUl .active").siblings();
            var brands = [];
            $.each($li, function (index, item) {
                var obj = {
                    id: $(item).attr('data-id'),
                    showIndex: $(item).attr('data-showIndex'),
                }
                brands.push(obj);
            });
            var $li = $(".productUl2 .active").siblings();
            var products = [];
            $.each($li, function (index, item) {
                var obj = {
                    id: $(item).attr('data-id'),
                    showIndex: $(item).attr('data-showIndex'),
                }
                products.push(obj);
            });
            var bigDto = { brands: brands, products: products }
            var params = JSON.stringify(bigDto);
            $.ajax({
                type: 'post',
                url: urls.updateShowIndex,
                data: params,
                dataType: 'json',
                cache: false,
                contentType: 'application/json',
                error: function () { },
                success: function (rs, textStatus) {
                    alert(msg.success);
                    // window.local.reload();
                    window.location.href = urls.orderByView;
                }
            });

        });
    }



    function init() {
        queryClassifyAll();
        update();
    };

    init();
});
